From 12d2dd9f7e6e866f87557ce9d0644eec0f4cd447 Mon Sep 17 00:00:00 2001
From: Vladislav Tarakanov <vladislav.tarakanov@bk.ru>
Date: Wed, 20 Mar 2024 23:32:08 +0400
Subject: [PATCH 3/6] Add user defined paper size to droplist

Change-Id: Iab086236103778dbb0c70f1a7feff158a83b930c
---
 vcl/source/window/printdlg.cxx | 31 +++++++++++++++++++++++++++----
 1 file changed, 27 insertions(+), 4 deletions(-)

diff --git a/vcl/source/window/printdlg.cxx b/vcl/source/window/printdlg.cxx
index bde4740d7920..d30895376f6c 100644
--- a/vcl/source/window/printdlg.cxx
+++ b/vcl/source/window/printdlg.cxx
@@ -904,8 +904,11 @@ void PrintDialog::setPaperSizes()
     }
     else
     {
-        Size aSizeOfPaper = aPrt->GetSizeOfPaper();
+        // Some printers return A4 size instread of user defined size (catch on CUPS Generic PDF printer)
+        Size aSizeOfPaper = getJobPageSize();/*aPrt->GetSizeOfPaper();*/
         PaperInfo aPaperInfo(aSizeOfPaper.getWidth(), aSizeOfPaper.getHeight());
+        // Needs to check is that existing paper size or defined by user
+        PaperInfo aTransparentPaperInfo(aSizeOfPaper.getHeight(), aSizeOfPaper.getWidth());
         const LocaleDataWrapper& rLocWrap(Application::GetSettings().GetLocaleDataWrapper());
         o3tl::Length eUnit = o3tl::Length::mm;
         int nDigits = 0;
@@ -914,6 +917,9 @@ void PrintDialog::setPaperSizes()
             eUnit = o3tl::Length::in100;
             nDigits = 2;
         }
+
+        bool bIsUserDefinedPaper = true;
+
         for (int nPaper = 0; nPaper < aPrt->GetPaperInfoCount(); nPaper++)
         {
             PaperInfo aInfo = aPrt->GetPaperInfo( nPaper );
@@ -937,9 +943,26 @@ void PrintDialog::setPaperSizes()
 
             mxPaperSizeBox->append_text(aPaperName);
 
-            if ( (ePaper != PAPER_USER && ePaper == mePaper) ||
-                 (ePaper == PAPER_USER && aInfo.sloppyEqual(aPaperInfo) ) )
-                 mxPaperSizeBox->set_active( nPaper );
+            // Some printers return "A4" instread of "PAPER_USER" (catch on CUPS Generic PDF printer).
+            // Since we need to add a user defined paper to list, the paper is now checked to equal in any cases
+            if ( aInfo.sloppyEqual(aPaperInfo) || aInfo.sloppyEqual(aTransparentPaperInfo) )
+            {
+                bIsUserDefinedPaper = false;
+                mxPaperSizeBox->set_active( nPaper );
+            }
+        }
+
+        // If paper size is defined by user, then it must be added to the list
+        if (bIsUserDefinedPaper)
+        {
+            Size aLogicPaperSize( o3tl::convert(aSizeOfPaper, o3tl::Length::mm100, eUnit) );
+            OUString aWidth( rLocWrap.getNum( aLogicPaperSize.Width(), nDigits ) );
+            OUString aHeight( rLocWrap.getNum( aLogicPaperSize.Height(), nDigits ) );
+            OUString aUnit = eUnit == o3tl::Length::mm ? OUString("mm") : OUString("in");
+            OUString aPaperName = "User defined: " + aWidth + aUnit + " x " + aHeight + aUnit;
+
+            mxPaperSizeBox->append_text(aPaperName);
+            mxPaperSizeBox->set_active( mxPaperSizeBox->get_count() - 1 );
         }
 
         mxPaperSizeBox->set_sensitive( true );
-- 
2.40.1

